﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Order.Entity;
using Runda.CMS.Order.Repository;

namespace Runda.CMS.Portal.Order
{
    public partial class OrderList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                BindList(1);
            }
        }

        private void BindList(int pageindex)
        {
            if (Request.QueryString["orderId"] != String.Empty)
            {
                OrderRepository orderRepository = new OrderRepository();
                int recordCount = 0;
                var list = orderRepository.findOrderinfo("", "", "", "", Request.QueryString["orderId"], AspNetPager1.PageSize, pageindex, out recordCount);

                RepeaterOrders.DataSource = list;
                RepeaterOrders.DataBind();
                AspNetPager1.RecordCount = recordCount;
            }
        }

        protected void RepeaterOrders_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
            if (e.CommandName == "delete")
            {
              
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindList(AspNetPager1.CurrentPageIndex);
        }
    
        protected void RepeaterOrders_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label shippingRequestId = e.Item.FindControl("shippingRequestId") as Label;
            Label deliverCompany = e.Item.FindControl("deliverCompany") as Label;
            Label customerID = e.Item.FindControl("customerID") as Label;
            Label inventoryCode = e.Item.FindControl("inventoryCode") as Label;
            Label billNo = e.Item.FindControl("billNo") as Label;
            Label deliverNum = e.Item.FindControl("deliverNum") as Label;
            Label preReceivedDate = e.Item.FindControl("preReceivedDate") as Label;
            Label receiveDate = e.Item.FindControl("receiveDate") as Label;
            Label receiveMan = e.Item.FindControl("receiveMan") as Label;
            if (shippingRequestId != null) {
                ShippingRequestRepository shippingRequestRepository = new ShippingRequestRepository();
                List<ShippingRequest> requests = shippingRequestRepository.getShippingRequestByOrderID(billNo.Text, inventoryCode.Text, customerID.Text);
                String requestId = "";
                foreach (ShippingRequest sr in requests) {
                   if (requestId != "") {
                        requestId += ";";
                    }
                    requestId += sr.requestId;
                    ShippingRepository shippingRepository = new ShippingRepository();
                    List<Shipping> shippings = shippingRepository.findShipping(sr.requestId, inventoryCode.Text, customerID.Text, billNo.Text);
                    if (shippings != null && shippings.Count>0) {
                        foreach (Shipping shipping in shippings) {
                            deliverCompany.Text = deliverCompany.Text + (deliverCompany.Text =="" ?"":";") + shipping.sendCompany;
                            deliverNum.Text = deliverNum.Text + (deliverNum.Text ==""?"":";") + shipping.sendBillNo;
                            preReceivedDate.Text = preReceivedDate.Text + (preReceivedDate.Text ==""?"":";") + shipping.preReceivedDate;
                            receiveMan.Text = receiveMan.Text + (receiveMan.Text == ""? "":";") + shipping.receivedMan;
                            receiveDate.Text = receiveDate.Text + (receiveDate.Text==""?"":";") + shipping.receivedDate;
                        }
                    }
               }
                shippingRequestId.Text = requestId;
            }
        } 
    }
}
